package com.fang.service;

import com.fang.mapper.UserMapper;
import com.fang.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@SuppressWarnings("all")
@Service
@Transactional  // 支持事务
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User queryUserByName(String name) {
        return null;
    }

    @Override
    public User login(User user) {
        User userDB = userMapper.queryUserByName(user.getUsername());
        if (userDB != null) {
            if (userDB.getPassword().equals(user.getPassword())) {
                return userDB;
            } else {
                throw new RuntimeException("密码错误");
            }
        } else {
            throw new RuntimeException("用户名不存在");
        }
    }

    @Override
    public void register(User user) {
        if (userMapper.queryUserByName(user.getUsername()) == null) {
            userMapper.register(user);
        } else {
            throw new RuntimeException("用户名已存在，请重新注册");
        }

    }
}
